#include <iostream>
using namespace std;

struct nod
{
    int info;
    struct nod *next, *prev;
};

typedef struct nod Tnod;
typedef Tnod* ref;

class matrice
{
    ref prim,ultim;
    int w,h;
public:
    matrice();
    matrice(int,int);
    ~matrice();
    void read();
    void print();
    void identica();
    void sort();
    void setPoint(int, int);
    matrice operator+(matrice);
    matrice operator*(matrice);
    matrice operator-(matrice);
    int operator[](int);
};
matrice::matrice(){}
matrice::matrice(int x, int y) : w(x),h(y)
{   
    ref q;
    q=new Tnod;
    q->info=0;
    prim=q;
    prim->next=0;
    prim->prev=0;
    ultim=prim;
    for (int i=0;i<w;i++)
        for (int j=0;j<h;j++)
        {
            ref r;
            r=new Tnod;
            r->info=0;
            ultim->next=r;
            r->prev=ultim;
            ultim=r;
            ultim->next=NULL;
        }
    prim=prim->next;
}
matrice::~matrice(){}

void matrice::read()
{
    cout<<"width:";
    cin>>w;
    cout<<"Height:";
    cin>>h;
    ref q;
    q=new Tnod;
    q->info=0;
    prim=q;
    prim->next=0;
    prim->prev=0;
    ultim=prim;
    for (int i=0;i<w*h;i++)
    {
        ref r;
        r=new Tnod;
        cout<<"Info:";
        cin>>r->info;
        ultim->next=r;
        r->prev=ultim;
        ultim=r;
        ultim->next=NULL;
    }
    prim=prim->next;
}

void matrice::print()
{
    ref r=prim;
    for (int i=0;i<w;i++)
    {
        for (int j=0;j<h;j++)
        {
            cout<<r->info<<' ';
            r=r->next;
        }
        cout<<endl;
    }
}

void matrice::identica()
{
    cout<<"width:";
    cin>>w;
    h=w;
    ref q;
    q=new Tnod;
    q->info=0;
    prim=q;
    prim->next=0;
    prim->prev=0;
    ultim=prim;
    for (int i=0;i<w;i++)
        for (int j=0;j<h;j++)
        {
            ref r;
            r=new Tnod;
            if (i==j)
                r->info=1;
            else
                r->info=0;
            ultim->next=r;
            r->prev=ultim;
            ultim=r;
            ultim->next=NULL;
        }
     prim=prim->next;
}

void matrice::sort()
{
    ref r=prim, q=prim;
    while (r!=NULL)
    {
        while (q!=NULL)
        {
           if (r->info < q->info)
            {
                int temp=r->info;
                r->info=q->info;
                q->info=temp;
            }
            q=q->next;
        }
        q=prim;
        r=r->next;
    }
}

void matrice::setPoint(int poz, int val)
{
    ref r=prim;
    int q=0;
    while(r!=NULL)
    {
        if (q==poz)
            r->info=val;
        r=r->next;
        q++;
    }
}
int matrice::operator[](int i)
{
    ref r=prim;
    int q=0;
    while (r!=NULL)
    {
        if (q==i)
            return r->info;
        r=r->next;
        q++;
    }
    return -1;
}
matrice matrice::operator+(matrice a)
{
    int x=w>a.w?w:a.w;
    int y=h>a.h?h:a.h;
    int minW=w<a.w?w:a.w;
    int minH=h<a.h?h:a.h;
    matrice result(x,y);
    
    for (int i=0;i<minW;i++)
        for (int j=0;j<minH;j++)
            setPoint(i+minW*j,(*this)[i+w*j]+a[i+a.w*j]);
    return result;
}

int main()
{
    matrice a,b;
    a.read();
    b.read();
    a=a+b;
    a.print();
}